package com.iretailer.web;

import com.alibaba.fastjson.JSON;
import com.iretailer.base.BaseController;
import com.iretailer.model.User;
import com.iretailer.util.MD5Util;
import com.iretailer.util.ResponeUtil;
import freemarker.template.utility.SecurityUtilities;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xjwtt
 * @date 2018/8/21
 */
@RestController
public class UserController extends BaseController {
    private Logger log = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/selectAll.action", method = RequestMethod.GET)
    public void selectAll(HttpServletResponse response, HttpServletRequest request) {
        List<User> all = userService.list(null);
        ResponeUtil.sendSuccess(response, all);
    }

    /**
     * 用户登录
     *
     * @param user
     */
    @PostMapping(value = "/login.action")
    public void login(HttpServletResponse response, HttpServletRequest request, @RequestBody User user) {

        Map<String, Object> result = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
//        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsercode(), MD5Util.get32Md5(user.getUserpwd()));
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsercode(), user.getUserpwd(), user.isRememberMe());
        try {
            subject.login(token);
            result.put("userInfo", subject.getPrincipal());
            result.put("toke", subject.getSession().getId());
            result.put("code", 200);
        } catch (IncorrectCredentialsException e) {
            result.put("msg", "密码错误");
        } catch (LockedAccountException e) {
            result.put("msg", "登录失败，用户被冻结");
        } catch (AuthenticationException e) {
            result.put("msg", "用户不存在");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponeUtil.writerObject(response, result);
    }

    






    /**
     *
     * @Author xjw
     * @Date 2018/10/10 10:28
     * @Description : 退出登录接口
     *
     **/
    @GetMapping(value = "/logout.action")
    public void logOut(HttpServletResponse response, HttpServletRequest request) {
        SecurityUtils.getSubject().logout();
        ResponeUtil.sendSuccess(response, true);
    }
}
